package com.easondongh.business.controller;

import com.easondongh.business.domain.Provider;
import com.easondongh.business.service.ProviderService;
import com.easondongh.business.vo.ProviderQueryVo;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/5/27 8:17
 */
@Slf4j
@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    /**
     * 新增供应商
     * @param provider
     * @return
     */
    @PostMapping("/addProvider")
    public ResponseObj addProvider(@RequestBody Provider provider) {
        if (provider == null || StringUtils.isBlank(provider.getProviderName()) || StringUtils.isBlank(provider.getConnectionPerson())
                || StringUtils.isBlank(provider.getPhone())) {
            log.warn("供应商：新增时供应商名称、联系人、联系电话均不能为空");
            return new ResponseObj(ResponseEnum.ADD_FAIL);
        }
        this.providerService.save(provider);
        return new ResponseObj(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 修改供应商
     * @param provider
     * @return
     */
    @PutMapping("updateProvider")
    public ResponseObj updateProvider(@RequestBody Provider provider) {
        if (provider == null || provider.getId() == null || StringUtils.isBlank(provider.getProviderName()) || StringUtils.isBlank(provider.getConnectionPerson())
                || StringUtils.isBlank(provider.getPhone())) {
            log.warn("供应商：更新时id、供应商名称、联系人、联系电话均不能为空");
            return new ResponseObj(ResponseEnum.UPDATE_FAIL);
        }
        this.providerService.updateById(provider);
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 批量删除供应商
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public ResponseObj deleteByIds(@RequestBody List<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            this.providerService.removeByIds(ids);
        }
        return new ResponseObj(ResponseEnum.DELETE_SUCCESS);
    }

    /**
     * 分页查询供应商
     * 用于后台查询，会将未启用的供应商页查询出来
     * @return
     */
    @PostMapping("/getByPage")
    public ResponseObj<PageResult<Provider>> getByPage(@RequestBody BasePageQueryVo<ProviderQueryVo> queryCondition) {
        if(queryCondition == null) {
            return ResponseObj.QueryFail;
        }
        PageResult<Provider> result = this.providerService.getByPage(queryCondition);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }

    @GetMapping("/getAllEnableProvider")
    public  ResponseObj<List<Provider>> getAllEnableProvider() {
        List<Provider> providers = this.providerService.getAllEnableProvider();
        return  new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, providers);
    }

    /**
     * 启用当前供应商
     * @param id
     * @return
     */
    @PutMapping("/enable")
    public ResponseObj enableById(Integer id) {
        if(id != null) {
            this.providerService.enableById(id);
        }
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 弃用当前供应商
     * @param id
     * @return
     */
    @PutMapping("/disable")
    public ResponseObj disableById(Integer id) {
        if(id != null) {
            this.providerService.disableById(id);
        }
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }
}
